
package com.jf.cloud.platform.controller;

import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.platform.model.SysConfig;
import com.jf.cloud.platform.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author chenlin
 * @DateTime: 2020/3/19 8:29
 * @description: 支付配置、文件上传配置、短信配置、快递配置、小程序配置、公众号配置
 */
@RestController
@RequestMapping("/p/sys_config")
@ConditionalOnProperty(prefix = "jfcloud", name = "expose.permission", havingValue = "true", matchIfMissing = true)
public class SysConfigController {


    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 获取保存配置信息
     */
    @GetMapping("/info/{key}")
    public ServerResponseEntity<String> info(@PathVariable("key")String key){
        return ServerResponseEntity.success(sysConfigService.getValue(key));
    }

    /**
     * 保存配置
     */
    @PostMapping("/save")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid SysConfig sysConfig){
        sysConfigService.saveOrUpdateSysConfig(sysConfig);
        return ServerResponseEntity.success(true);
    }

    @PostMapping("/saveDeliveryConfig")
    @Operation(summary = "保存物流查询配置信息" , description = "保存物流查询配置信息")
    public ServerResponseEntity<Boolean> saveDeliveryConfig(@RequestBody @Valid SysConfig sysConfig) {
        List<String> invalidKeys = sysConfigService.saveDeliveryConfig(sysConfig);
        invalidKeys.forEach(key -> {
            sysConfigService.removeSysConfig(key);
        });
        return ServerResponseEntity.success(true);
    }


}
